package com.lemon.smartsafetynew.ui.adapter.record;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.record.GridInfoDo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2018/1/11.
 */

public class RecordGridListAdapter extends LemonRcvAdapter<GridInfoDo> {

    private RecyclerTClickListener<GridInfoDo> mListener;

    public RecordGridListAdapter(@Nullable List<GridInfoDo> data, @Nullable RecyclerTClickListener<GridInfoDo> mListener) {

        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    public class ItemAdapter implements AdapterItem<GridInfoDo> {

        private GridInfoDo data;
        private int mPosition;

        @Bind(R.id.search_tv)
        TextView searchTv;
        @Bind(R.id.historyClean)
        TextView historyClean;
        @Bind(R.id.history_layout)
        LinearLayout historyLayout;
        @Bind(R.id.grid_name_tv)
        TextView gridNameTv;
        @Bind(R.id.view_line)
        View viewLine;
        @Bind(R.id.common_choose_item_layout)
        LinearLayout commonChooseItemLayout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_grid_manager;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {

            commonChooseItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!SafeCloudConstants.VIEW_TYPE.equals(data.getGrid_id()) &&
                            !SafeCloudConstants.HISTORY_NAME.equals(data.getGrid_name())) {
                        mListener.onItemClick(data, mPosition);
                    }
                }
            });

            historyClean.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewEvent event = new ViewEvent(RecordGridListAdapter.class.getName());
                    AppService.getBus().post(event);
                }
            });
        }

        @Override
        public void handleData(GridInfoDo gridInfoDo, int position) {

            data = gridInfoDo;
            mPosition = position;

            if (SafeCloudConstants.VIEW_TYPE.equals(gridInfoDo.getView_type()) &&
                    SafeCloudConstants.HISTORY_NAME.equals(gridInfoDo.getGrid_name())) {

                gridNameTv.setVisibility(View.GONE);
                historyLayout.setVisibility(View.VISIBLE);
                searchTv.setText(CommonUtil.avoidNullString(gridInfoDo.getGrid_name()));

            } else {

                gridNameTv.setVisibility(View.VISIBLE);
                historyLayout.setVisibility(View.GONE);
                gridNameTv.setText(CommonUtil.avoidNullString(gridInfoDo.getGrid_name()));

            }


        }

    }

}
